//
//  LIUViewController.m
//  RxLogin
//
//  Created by liuyu on 15/12/16.
//  Copyright (c) 2016 limit. All rights reserved.
//

#import "LIUViewController.h"
#import "LIULoginViewModel.h"

@interface LIUViewController ()

/** 账户框 */
@property (weak, nonatomic) IBOutlet UITextField *account;
/** 密码框 */
@property (weak, nonatomic) IBOutlet UITextField *password;
/** 登录按钮 */
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

/** 登录控制视图模型 */
@property (nonatomic, strong) LIULoginViewModel *loginVm;
@end

@implementation LIUViewController

#pragma mark - lazy

- (LIULoginViewModel *)loginVm {
    if (!_loginVm) {
        _loginVm = [[LIULoginViewModel alloc] init];
    }
    return _loginVm;
}

#pragma mark - life cycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self viewBindSignal];
    [self sendLoginRequest];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - events

/**
 *  绑定视图信号
 */
- (void)viewBindSignal {
    RAC(self.loginVm, account) = _account.rac_textSignal;
    RAC(_loginVm, password) = _password.rac_textSignal;
    RAC(_loginBtn, enabled) = _loginVm.enableLoginSignal;
}
/**
 *  登录请求
 */
- (void)sendLoginRequest {
    @weakify(self)
    [[self.loginBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        @strongify(self)
        [self.loginVm.command execute:nil];
    }];
}
@end
